package interfaces;

import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DebugHandler implements InvocationHandler {
	
	private static UserInterface bank;
	public static Logger logger = Logger.getLogger(BankProxy.class.getName());
	
	public static ConsoleHandler consoleHandler = new ConsoleHandler();

	public DebugHandler(UserInterface b){
		DebugHandler.bank = b;
		logger.setLevel(Level.FINE);
	}
	
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
		
		System.out.println("* calling method " + method + "with params");
		logger.log(Level.INFO, "blubb");
		logger.addHandler(consoleHandler);
		
		for (int i = 0; i < args.length; i++)
			System.out.print(" " + args[i]);
			System.out.println();
			
		Object result = null;
		
		try{
			result = method.invoke(bank, args);
			
		
		} catch(IllegalAccessException ex)  {
            ex.printStackTrace();
        } catch(InvocationTargetException ex)  {
            System.out.println("* exception:" + ex.getTargetException());
            throw ex.getTargetException();
        }
		
        System.out.println("* result: " + result);
        return result;
	}
	
	
	
	
	public static void addHandler(){
		logger.addHandler(consoleHandler);
		consoleHandler.setLevel(Level.FINEST);

	}

}
